{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d00f7300",
   "metadata": {},
   "source": [
    "---\n",
    "* 记录人（学习人）：曾紫涵\n",
    "* 时间：2021-11-15\n",
    "* week12\n",
    "* 学习线上资源：[python官方文档]()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd8b808b",
   "metadata": {},
   "source": [
    "## 本周知识点\n",
    "\n",
    "### 1.字典的一般方法\n",
    "\n",
    "> 1. 字典的查询 查询key-> value\n",
    ">> 1. 先看字典的key 是否存在：in 查询成员是否存在的重要方法\n",
    ">> 2. 查询\n",
    "> 2. 字典的修改\n",
    "> 3. 字典的删除\n",
    "> 4. 字典的增加 (dict_name[key] = value)\n",
    "\n",
    "### 2. 字典的进阶方法\n",
    "\n",
    "> 1. 字典的初始化\n",
    ">> 1. D.fromkeys()\n",
    ">> 2. D.setdefault()\n",
    "\n",
    "### 3. 字典的实践（一）课本\n",
    "\n",
    "### 4. 字典的实践（二）课本\n",
    "\n",
    "### 5. 字典的实践（三）课本\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a0e7794",
   "metadata": {},
   "source": [
    "## 1.字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d710516b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'Xiaofeng', 'stu1803': 'Lihua'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"Xiaofeng\",\n",
    "    \"stu1803\":\"Lihua\"\n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "345b54f5",
   "metadata": {},
   "outputs": [],
   "source": [
    ">1. 字典的查询\n",
    ">> 1. 直接查找 dict_name[key] 当关键词key不存在时,报错：keyError:'stu1804'\n",
    ">> 2. get()方法 D.get(k[,d]) 当关键词key不存在时，返回None不存在。不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b88acdc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接查找\n",
    "stu_info[\"stu1801\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "494fdd81",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1804'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-af87bde9363c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"stu1804\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'stu1804'"
     ]
    }
   ],
   "source": [
    "stu_info[\"stu1804\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4625a018",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function get:\n",
      "\n",
      "get(key, default=None, /) method of builtins.dict instance\n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 使用get方法查询\n",
    "help(stu_info.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e29da693",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8201ed03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stu_info.get(\"stu1804\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec8d36df",
   "metadata": {},
   "source": [
    "> 2. 修改 & 新增 \n",
    ">> 1. 先查询 查看key是否存在 使用 in 查询成员\n",
    ">> 2. 如果key存在 修改：dict_name[key] = value\n",
    ">> 3. 如果key不存在 新增：dict_name[key] = value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6fb70d2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 stu1804\n",
    "\"stu1804\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c9dd797e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 stu1803\n",
    "\"stu1803\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cf0c0397",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'Xiaofeng', 'stu1803': 'Liuyu'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果key存在 修改 \"'stu1803': 'Lihua'--> 'stu1803':\"Liuyu\"\n",
    "if \"stu1803\" in stu_info:\n",
    "    stu_info[\"stu1803\"] = \"Liuyu\"\n",
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6444b57f",
   "metadata": {},
   "source": [
    "> 3. 字典的删除\n",
    ">> 1. D.pop() 的一般操作如下\n",
    ">>> 1. 先查询是否存在\n",
    ">>> 2. 再使用删除方法\n",
    ">> 2. D.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e972c195",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c8e7b2d7",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pop expected at least 1 argument, got 0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-e01f3845ec96>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#  不正确， 不同于列表 （删除最后一个）\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: pop expected at least 1 argument, got 0"
     ]
    }
   ],
   "source": [
    "stu_info.pop() #  不正确， 不同于列表 （删除最后一个）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7034add1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典删除的正确使用\n",
    "if 'stu1803' in stu_info:\n",
    "    stu_info.pop('stu1803')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c8d8214d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'Xiaofeng'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5005b7bf",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu180'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-c9c5d884ce1d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'stu180'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'stu180'"
     ]
    }
   ],
   "source": [
    "# 如果不先查询 key是否存在，有可能报错： KeyError: 'stu180'\n",
    "stu_info.pop('stu180')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7cd05ddd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "afa41b48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1802', 'Xiaofeng')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "9c05d4ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ff87059",
   "metadata": {},
   "source": [
    "## 字典的实践（一）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb1727ac",
   "metadata": {},
   "outputs": [],
   "source": [
    " vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    " word = input(\"Provide a word to search for vowels:\")\n",
    " found = {}\n",
    " \n",
    " # found[\"a\"]=0\n",
    " # found[\"e\"]=0\n",
    " # found[\"i\"]=0\n",
    " # found[\"o\"]=0\n",
    " # found[\"u\"]=0\n",
    " found = found.fromkeys(vowels,0)\n",
    " \n",
    " for letter in word:\n",
    "     if letter in vowels:\n",
    "         found[letter] += 1\n",
    " \n",
    " # print(found.items())\n",
    " for k,v in found.items():\n",
    "     print(k,\"was found\",v,\"time(s)\")\n",
    " # print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ea5ec73",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 目标 :查询 元音字母是否出现在word文本当中，并且统计出现的次数\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 字典 初始化 的值\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "\n",
    "for i in vowels:\n",
    "    #　使用字典的新增　构建初始化字典\n",
    "    found[i] = 0 \n",
    "    \n",
    "# 循环用户输入的文本 ，出现一次元音字符，对应的元音字符+1 \n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1 \n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fa90f42",
   "metadata": {},
   "source": [
    "> 字典的进阶方法：初始化所有值fromkeys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc55fc34",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8093e2c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "#  word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 初始化 字典\n",
    "found.fronkeys(vowels)\n",
    "\n",
    "for iin word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b592b6f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d27d2167",
   "metadata": {},
   "source": [
    "> 字典的进阶方法： 初始化特定的值 setdefault()\n",
    "\n",
    ">> 1. 查询key是否出现在字典当中\n",
    ">>> 1. 出现过，就当没看见，不做任何操作\n",
    ">>> 2. key未出现过，给定key及value（指定的初始化的值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69f05555",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(found.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ef4e6b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password = {\n",
    "    \"XiaoMing\":\"222333\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07940d23",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password.setfault(\"lihua\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c9a91e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password.setdefault(\"xiaohong\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5622d65",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password.setdefault(\"XiaoMing\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ad34bb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d1024bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 字典的实践（二）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ad7d876",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 优化 ： 实践（一）\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found.setdefault(i,0)\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8156c6d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "{'e':1,'o':2,'i':1,'a':1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10e6da56",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 作业\n",
    "> 使用help查看字典的其他方法 \n",
    "> 尝试每一种方法的使用并实践\n",
    "> 用表格记录每一种方法的如何使用、注意事项、使用结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37e956f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 字典的实践（三）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
